package com.punai.customer.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.customer.domain.CusMaterialChapter;
import com.punai.customer.domain.CusMaterialItem;
import com.punai.customer.domain.CusMechanicalChapter;
import com.punai.customer.domain.CusMechanicalItem;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.domain.vo.CusAllVo;
import com.punai.customer.mapper.*;
import com.punai.customer.service.ICusAllService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CusAllServiceImpl implements ICusAllService {
    // 标准
    @Autowired
    private CusGxChapterMapper cusGxChapterMapper;
    @Autowired
    private CusGxFeeMapper cusGxFeeMapper;
    @Autowired
    private CusMaterialChapterMapper cusMaterialChapterMapper;
    @Autowired
    private CusMaterialItemMapper cusMaterialItemMapper;
    @Autowired
    private CusMechanicalChapterMapper cusMechanicalChapterMapper;
    @Autowired
    private CusMechanicalItemMapper cusMechanicalItemMapper;
    @Autowired
    private CusFbChapterMapper cusFbChapterMapper;
    @Autowired
    private CusFbFeeMapper cusFbFeeMapper;

    @Override
    public List<CusAllVo> selectAllCount(CusAllVo cusAllVo,Long companyId) {
        List<CusAllVo> list = new LinkedList<>();

        // 标准
        List<CusAllVo> gxNodeCounts = cusGxChapterMapper.countNode(companyId);
        List<CusAllVo> gxFeeCounts = cusGxFeeMapper.countFee(companyId);

        Map<String, Integer> gxNodeCountMap = gxNodeCounts.stream().collect(Collectors.toMap(CusAllVo::getSpecId, CusAllVo::getNodeCount));
        Map<String, Integer> gxFeeCountMap = gxFeeCounts.stream().collect(Collectors.toMap(CusAllVo::getSpecId, CusAllVo::getFeeCount));

        for (String specId : gxNodeCountMap.keySet()) {
            CusAllVo cusGx = new CusAllVo();
            cusGx.setName(specId);
            cusGx.setType(CusConstants.GX_TYPE);
            cusGx.setTypeName(CusConstants.GX_NAME);
            cusGx.setSpecId(specId);
            cusGx.setNodeCount(gxNodeCountMap.get(specId));
            cusGx.setFeeCount(gxFeeCountMap.get(specId));
            list.add(cusGx);
        }

        // 成本

        List<CusAllVo> fbNodeCounts = cusFbChapterMapper.countNode(companyId);
        List<CusAllVo> fbFeeCounts = cusFbFeeMapper.countFee(companyId);

        Map<String, Integer> fbNodeCountMap = fbNodeCounts.stream().collect(Collectors.toMap(CusAllVo::getSpecId, CusAllVo::getNodeCount));
        Map<String, Integer> fbFeeCountMap = fbFeeCounts.stream().collect(Collectors.toMap(CusAllVo::getSpecId, CusAllVo::getFeeCount));

        for (String specId : fbNodeCountMap.keySet()) {
            CusAllVo cusFb = new CusAllVo();
            cusFb.setName(specId);
            cusFb.setType(CusConstants.FB_TYPE);
            cusFb.setTypeName(CusConstants.FB_NAME);
            cusFb.setSpecId(specId);
            cusFb.setNodeCount(fbNodeCountMap.get(specId));
            cusFb.setFeeCount(fbFeeCountMap.get(specId));
            list.add(cusFb);
        }

        Long clNodeCount = cusMaterialChapterMapper.selectCount(
                Wrappers.<CusMaterialChapter>lambdaQuery()
                        .eq(CusMaterialChapter::getCompanyId, companyId)
                        .eq(CusMaterialChapter::getPid, CusConstants.DEF_PID)
        );
        Long clFeeCount = cusMaterialItemMapper.selectCount(
                Wrappers.<CusMaterialItem>lambdaQuery()
                        .eq(CusMaterialItem::getCompanyId, companyId)
        );

        CusAllVo cusCl = new CusAllVo();
        cusCl.setName(CusConstants.CL_NAME);
        cusCl.setType(CusConstants.CL_TYPE);
        cusCl.setTypeName(CusConstants.CL_NAME);
        cusCl.setNodeCount(Integer.parseInt(clNodeCount.toString()));
        cusCl.setFeeCount(Integer.parseInt(clFeeCount.toString()));
        list.add(cusCl);

        Long jxNodeCount = cusMechanicalChapterMapper.selectCount(
                Wrappers.<CusMechanicalChapter>lambdaQuery()
                        .eq(CusMechanicalChapter::getCompanyId, companyId)
                        .eq(CusMechanicalChapter::getPid, CusConstants.DEF_PID)
        );
        Long jxFeeCount = cusMechanicalItemMapper.selectCount(
                Wrappers.<CusMechanicalItem>lambdaQuery()
                        .eq(CusMechanicalItem::getCompanyId, companyId)
        );

        CusAllVo cusJx = new CusAllVo();
        cusJx.setName(CusConstants.JX_NAME);
        cusJx.setType(CusConstants.JX_TYPE);
        cusJx.setTypeName(CusConstants.JX_NAME);
        cusJx.setNodeCount(Integer.parseInt(jxNodeCount.toString()));
        cusJx.setFeeCount(Integer.parseInt(jxFeeCount.toString()));
        list.add(cusJx);

        return list;
    }
}
